<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="rolelist"
              :highlight-current-row="true"
              current-row-key="id"
              stripe
            >
              <el-table-column sortable label="序号" width="120" type="index" />
              <el-table-column
                sortable
                label="角色名称"
                width="240"
                prop="name"
              />
              <el-table-column sortable label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button
                    size="small"
                    type="success"
                    @click="showAssignDialog(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click.native="handleEditFn(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="handleDelFn(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                :page-sizes="page.pagesizes"
                :page-size="page.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top:50px"
              :model="companyInfo"
            >
              <el-form-item label="公司名称" prop="name">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址" prop="companyAddress">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱" prop="mailbox">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注" prop="remarks">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <AddRole
      :show-dialog.sync="showDialog"
      :form-data="formData"
      @reload="getRoleFn"
    />
    <AssignPermission
      :show-perm-dialog.sync="assign.showPermDialog"
      :role-id="assign.id"
    ></AssignPermission>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import {
  getRoleAPI,
  delRoleAPI,
  getCompanyInfoAPI,
  getRoleInfoAPI
} from '@/api/settings'
import AddRole from './components/add-role.vue'
import AssignPermission from './components/assign-permission.vue'
import { IRole } from '@/model/interface/role'
import { IPage } from '@/model/interface/request'
import { UserModule } from '@/store/modules/user'

@Component({ name: 'Setting', components: { AddRole, AssignPermission } })
export default class extends Vue {
  rolelist = [] // 角色列表
  companyInfo = {} // 公司信息
  showDialog = false // 弹出层
  formData: IRole = {
    name: '',
    description: ''
  } // 表单数据

  page: IPage = {
    page: 1, // 页码
    total: 0, // 总条数
    pagesize: 4, // 页容量
    pagesizes: [2, 4] // 页容量数组
  } // 分页数据

  assign = {
    id: '0',
    showPermDialog: false
  } // 权限弹层数据

  get companyId() {
    return UserModule.companyId
  }

  // 页码数据变化 - 重新获取列表
  @Watch('page', { deep: true })
  onPageChange() {
    this.getRoleFn()
  }

  // 弹层关闭 - 表单清空
  @Watch('showDialog')
  onPopuChange(val: boolean) {
    if (!val) this.formData = { name: '', description: '' }
  }

  created() {
    this.getRoleFn()
    this.getCompanyInfoFn()
  }

  // 打开权限弹层
  showAssignDialog(id: string) {
    this.assign = {
      id,
      showPermDialog: true
    }
  }

  // 获取公司信息
  async getCompanyInfoFn() {
    const res = await getCompanyInfoAPI(this.companyId)
    // console.log(res)
    this.companyInfo = res
  }

  // 获取列表
  async getRoleFn() {
    const { pagesize, page } = this.page
    const { rows, total } = await getRoleAPI({ page, pagesize })
    // console.log(res)
    this.rolelist = rows // 列表数据
    this.page.total = total // 总条数
    if ((this.page.pagesizes as Array<number>).indexOf(total) === -1) {
      (this.page.pagesizes as Array<number>).push(total) // 把总条数加入页容量可选项
    }
  }

  // 页容量改变
  handleSizeChange(val: number) {
    this.page.pagesize = val
  }

  // 页码改变
  handleCurrentChange(val: number) {
    this.page.page = val
  }

  // 删除
  handleDelFn(id: string) {
    // console.log(id)
    this.$confirm('此操作将永久删除, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        this.$message.success('删除成功!')
        delRoleAPI(id)
        this.page.page = 1 // 重置页码并刷新数据
      })
      .catch(() => this.$message.info('取消删除'))
  }

  // 编辑
  async handleEditFn(id: string) {
    const res = await getRoleInfoAPI(id)
    // console.log(res)
    this.formData = res // 数据回显
    this.showDialog = true // 打开弹层
  }
}
</script>
